package com.example;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.Date;

public class test {

    public static void main(String[] args) {
        // 生成或获取密钥
        SecretKey key = Keys.hmacShaKeyFor("chenchaungze123123jdhifahifiasfiashfieugisaibiauidasfiduhffhasufue".getBytes());

        // 要解析的JWT令牌
        String token = "eyJhbGciOiJIUzUxMiJ9.eyJuYW1lIjoi6L6f5byA5oWnIiwiaWQiOjIsInVzZXJuYW1lIjoi5rGk5rCRIiwiZXhwIjoxNzQ0NTUwMDIxfQ.Hyc4euue67xISv0_RUuxabKLeljomc1EZXzdr2TtkRx-SK2tReSuGn3M1if0kEECp13DjvhabqG0adhp-ROTMQ";

        // 解析JWT令牌
        Claims claims = Jwts.parserBuilder()
                .setSigningKey(key)
                .build()
                .parseClaimsJws(token)
                .getBody();

        // 输出解析后的声明
        System.out.println("Parsed claims: " + claims);
        System.out.println("Name: " + claims.get("name"));
        System.out.println("Id: " + claims.get("id"));
        System.out.println("Username: " + claims.get("username"));
        System.out.println("Expiration: " + new Date(claims.getExpiration().getTime()));
    }
}
